package com.transsion.clouddemo.core.config

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.ui.res.colorResource
import androidx.recyclerview.widget.RecyclerView
import com.transsion.clouddemo.CloudApplication.Companion.context
import com.transsion.clouddemo.R
import kotlin.coroutines.coroutineContext

class LogAdapter(private val logList: MutableList<LogEntry>) : RecyclerView.Adapter<LogAdapter.LogViewHolder>() {

    inner class LogViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val logTextView: TextView = itemView.findViewById(R.id.log_text)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.log_item, parent, false)
        return LogViewHolder(view)
    }

    override fun onBindViewHolder(holder: LogViewHolder, position: Int) {
        textView(holder).text = logList[position].message
        if (logList[position].message.contains("zhangxin")) {
            holder.logTextView.setTextColor(context.getColor(R.color.cloud_text_selected))
        } else if (logList[position].message.contains("okhttp.OkHttpClient")){
            holder.logTextView.setTextColor(Color.YELLOW)
        }
    }

    private fun textView(holder: LogViewHolder) =
        holder.logTextView

    override fun getItemCount(): Int = logList.size

}